package com.baselibrary.libs.View.defined.defined_viewpager_banner;

import android.content.Context;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.baselibrary.libs.R;
import com.baselibrary.libs.Utils.log.LogUtils;
import com.baselibrary.libs.View.My.MyViewPger;
import com.baselibrary.libs.View.baseView.BaseView;
import com.baselibrary.libs.base.baseAdapter.BaseViewPagerAdapter;
import com.baselibrary.libs.example.example_ViewPage_banner.adapter.ImageAdapter;
import com.baselibrary.libs.example.example_ViewPage_banner.utils.intctorUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 35876 于萌萌
 * 创建日期: 12:21 . 2017年02月02日
 * 描述:广告图 适配器
 * <p>
 * <p>
 * 备注:
 */

public class Defined_ViewPager_Banner extends BaseView {

    private MyViewPger viewpager_banner;
    private ImageAdapter mAdapter;
    private ImageView iv_red_point;
    private LinearLayout ll;
    private ImageView point_red;
    List list = new ArrayList();
    public Defined_ViewPager_Banner(Context context) {
        super(context);
    }

    public Defined_ViewPager_Banner(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public Defined_ViewPager_Banner(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public int BANNER_LOOP_TIME = 5000;
    private Handler mHandler = new Handler();
    //当前选中的banner 索引
    private int currentBannerPos = 0;
    private Runnable loopRunnable = new Runnable() {

        @Override
        public void run() {
            LogUtils.i("开始轮询里面"+currentBannerPos);
            if(++currentBannerPos>=list.size()){
                currentBannerPos = 0;
            }
            viewpager_banner.setCurrentItem(currentBannerPos);
            mHandler.postDelayed(loopRunnable, BANNER_LOOP_TIME);
        }
    };

    @Override
    public int getXml() {
        return R.layout.defined_viewpager_banner;
    }

    @Override
    public void initView(View view) {
        viewpager_banner = (MyViewPger)view.findViewById(R.id.banner);

        ll = (LinearLayout) findViewById(R.id.ll);
        point_red = (ImageView) findViewById(R.id.point_red);

    }

    public ImageAdapter getAdapter() {
        return mAdapter;
    }

    public MyViewPger getViewpager_banner() {
        return viewpager_banner;
    }

    public Defined_ViewPager_Banner initData(Context context, List list, BaseViewPagerAdapter.CallBack callBack){
        this.list = list;
        mAdapter = new ImageAdapter(context, list, callBack);
        viewpager_banner.setAdapter(mAdapter);

        ll.removeAllViews();
        if (list.size()==0||list==null){
            ll.setVisibility(GONE);
            return this;
        }
        intctorUtils.startDrawable(context, (ArrayList) list,ll,viewpager_banner,point_red );
        mHandler.postDelayed(loopRunnable, BANNER_LOOP_TIME);
        LogUtils.i("开始轮询"+currentBannerPos);

        viewpager_banner.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentBannerPos = position;
                mHandler.removeCallbacks(loopRunnable);
                mHandler.postDelayed(loopRunnable, BANNER_LOOP_TIME);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        return this;
    }

}
